﻿package org.hyy.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hyy.dao.UserDAO;
import org.hyy.vo.User;

public class InsertOne extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("GBK");
		//取参数
		String username = request.getParameter("username");
		String password = request.getParameter("passwd");
		String phone = request.getParameter("phone");
		String addr = request.getParameter("addr");
		HttpSession session = request.getSession();
		//封装到类
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setPhone(phone);
		user.setAddr(addr);
		user.setRdate(new Date());
		//添加记录
		UserDAO userDAO = new UserDAO();
		boolean result = userDAO.insertOne(user);

		//参数返回
		if(!result){
			//成功
			session.setAttribute("show", "reg");
			response.sendRedirect("success.jsp");
		}
		else{
			//失败
			response.sendRedirect("regerr.jsp");
		}
	}

}
